package com.dwxt.cdms.scheduling;

import com.dwxt.cdms.entity.Equipment;
import com.dwxt.cdms.service.EquipmentService;
import com.dwxt.cdms.service.TaskService;
import com.dwxt.common.util.JsonUtils;
import com.dwxt.common.util.OkHttpUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
@EnableScheduling
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Slf4j
public class Task {

    private final static Logger logger = LoggerFactory.getLogger(Task.class);

    @Autowired
    private EquipmentService equipmentService;

    @Autowired
    private TaskService taskService;

    //半小时拉取一次手表数据
    //@Scheduled(cron = "0 0/30 * * * ? ")
    //@Scheduled(cron = "0 0/1 * * * ? ")
    //@Async("processExecutor")
    public void pullWatchData() {
        logger.info("=======手环定时任务启动=======");
        //System.out.println(Thread.currentThread().getName());
        List<Equipment> equipmentList = equipmentService.selectEquipmentByCategory(5);
        if (equipmentList.size() == 0) {
            return;
        }
        String url = "http://api.91ganlu.com/watch/get/watch/info";
        List<String> collect = equipmentList.stream().map(Equipment::getSerialNumber).collect(Collectors.toList());
        Map params = new HashMap();
        params.put("list", collect);

        Map headers = new HashMap();
        headers.put("accessToken", "2344496fe38c429fb79f6394812c8130");
        String result = OkHttpUtils.jsonPost(url, JsonUtils.objectToJson(params), headers);
        taskService.pullWatchData(result);
    }

    //每天生成超过季度报告 和年度报告
    @Scheduled(cron = "0 0 0 * * ? ")
    @Async("processExecutor")
    public void generateTask() {
        taskService.generateTask();
    }


    // 每隔10秒查询数据库 推送异常数据
    @Scheduled(cron = "0/15 * * * * ?")
//    @Scheduled(cron = "0 * * * * ?")
    @Async("processExecutor")
    public void putMessage() {
        try {
            taskService.putMessage();
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }



    /**
     * 每天删除和新增生日快到的customer
     */
    @Scheduled(cron = "0 0 0 * * ? ")
    //@Scheduled(cron = "0 0/1 * * * ? ")
    @Async("processExecutor")
    public void addAndDeleteBirthday() {
        taskService.addAndDeleteBirthday();
    }



    /**
     * 每天删除旧的oufu手环睡眠数据
     */
    @Scheduled(cron = "0 0 3 * * ? ")
    @Async("processExecutor")
    public void deleteOufuBandSleep() {
        taskService.deleteOufuBandSleep();
    }


    /**
     * 来邦socket发送心跳
     */
    @Scheduled(cron = "0/13 * * * * ? ")
    @Async("processExecutor")
    public void lbKeepAliveTask() {
        taskService.lbKeepAlive();
    }


    /**
     * 来邦手表入库每天的步数（23点左右上报）
     */
    @Scheduled(cron = "0 1 0 * * ? ")
    @Async("processExecutor")
    public void lbWatchInsertStep() {
        taskService.lbWatchInsertStep();
    }

    /**
     * 来邦手表入库每天的睡眠（8、9点上报）
     */
    @Scheduled(cron = "0 0 10 * * ? ")
    @Async("processExecutor")
    public void lbWatchInsertSleep() {
        taskService.lbWatchInsertSleep();
    }

    /**
     * 来邦手表每个小时更新定位
     */
    @Scheduled(cron = "0 0 0/1 * * ? ")
    @Async("processExecutor")
    public void lbWatchInsertLoc() {
        taskService.lbWatchInsertLoc();
    }


    /**
     * 每月5号生成上月健康报告
     */
    @Scheduled(cron = "0 0 0 5 * ? ")
    @Async("processExecutor")
    public void CreateHealthReport() {
        taskService.createHealthReport();
    }


    /**
     * 每天发送生日祝福
     */
    @Scheduled(cron = "0 0 8 * * ? ")
    @Async("processExecutor")
    public void sendBirthdayMessage() {
        taskService.sendBirthdayMessage();
    }

    /**
     * 发送节日祝福
     */
    @Scheduled(cron = "0 0 8 * * ? ")
    @Async("processExecutor")
    public void sendFestivalMessage() throws ParseException {
        taskService.sendFestivalMessage();
    }


    /**
     * 72小时无数据用户存到redis
     */
    @Scheduled(cron = "0 0 0/1 * * ? ")
    @Async("processExecutor")
    public void save72HourNoDataCustomerToRedis()  {
        taskService.save72HourNoDataCustomerToRedis();
    }



    /**
     * 社区数据更新
     */
    @Scheduled(cron = "0 0 0-2 * * ? ")
    @Async("processExecutor")
    public void updateCommunityData()  {
        taskService.updateCommunityData();
    }
}
